USE [AnotherSuccessfulProcedure]

IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id(N'[dbo].[FlagEventSelectTop5PlayersByEventType]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
BEGIN
	DROP PROCEDURE dbo.FlagEventSelectTop5PlayersByEventType
END


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Neil Wilson
-- Create date: 09/03/08
-- Description:	Flag Event Table Select - Top 5 of a given Flag Event Type
-- =============================================
CREATE PROCEDURE FlagEventSelectTop5PlayersByEventType
	@FlagEventType varchar(30)
AS

BEGIN

DECLARE @FlagEventId int
SELECT @FlagEventId = FlagEventTypeId FROM FlagEventType WHERE FlagEventShortName = @FlagEventType

SELECT TOP 5 PlayerName, COUNT(FlagId) AS [FlagEvents], Flag.PlayerId
FROM Flag INNER JOIN PlayerNameList on Flag.Playerid = PlayerNameList.PlayerId
WHERE FlagEventId = @FlagEventId
GROUP BY PlayerName, Flag.PlayerId
ORDER BY [FlagEvents] DESC

END
GO

--  FlagEventSelectTop5PlayersByEventType 'capture'
--  FlagEventSelectTop5PlayersByEventType 'picked up'
--  FlagEventSelectTop5PlayersByEventType 'defended'
--  FlagEventSelectTop5PlayersByEventType 'dropped'
